import moment from 'dayjs';

function addOneDay(currentDay) {
	const d = new Date(currentDay);
	return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
}

/*将字符串类型时间转换为yyyy-MM-dd格式*/
function simpleDate(oldDate) {
	let dateArr = oldDate.split('-');
	dateArr['1'] = dateArr['1'].length == 1 ? '0' + dateArr['1'] : dateArr['1'];
	dateArr['2'] = dateArr['2'].length == 1 ? '0' + dateArr['2'] : dateArr['2'];

	return dateArr.join('-');
}

export const getEveryDay = (startDate, endData) => {
	const now = getNowMinutesAndSeconds();
	const oneDay = getOneDayMinutesAndSeconds();

	let dateList = [];
	let dayCount = (new Date(endData) - new Date(startDate)) / (1000 * 60 * 60 * 24);

	for (let i = 0; i < dayCount; i++) {
		const singleDate = startDate + i * (1000 * 60 * 60 * 24);

		const currentDay = simpleDate(addOneDay(singleDate));

		const params = {
			label: currentDay,
			children: !i ? now : oneDay
		};

		dateList.push(params);
	}
	return dateList;
};

const maxLength = 48;
const halfAnHour = 1000 * 60 * 30;

const getNowMinutesAndSeconds = function () {
	const date = new Date();

	const start = moment(date);
	const remainder = 30 - (start.minute() % 30);

	let startTime = new Date(date.toLocaleDateString()).getTime();
	let dateTime = moment(start).add(remainder, 'minutes').$d.getTime();
	const endTime = new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;

	const period = Array.from({ length: maxLength }, (e, index) => {
		if (startTime < date.getTime()) {
			startTime += halfAnHour;
			return;
		}

		const singleStartTime = moment(dateTime).format('HH:mm');

		dateTime += halfAnHour;
		const endValue = index + 1 === maxLength ? endTime : dateTime;

		const singleEndTime = moment(endValue).format('HH:mm');

		return {
			label: `${singleStartTime}~${singleEndTime}`
		};
	}).filter(Boolean);

	return period;
};

const getOneDayMinutesAndSeconds = function () {
	const date = new Date();

	const startTime = new Date(date.toLocaleDateString()).getTime();
	const endTime = new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;

	const period = Array.from({ length: maxLength }, (e, index) => {
		const singleStartTime = moment(startTime + index * halfAnHour).format('HH:mm');

		const endValue = index + 1 === maxLength ? endTime : startTime + (index + 1) * halfAnHour;
		const singleEndTime = moment(endValue).format('HH:mm');

		return {
			label: `${singleStartTime}~${singleEndTime}`
		};
	});

	return period;
};

/**
 * 判断 探店时间 是否小于 预计发刊时间
 */
export const isTandian = function (start, end) {
	const newStart = start
		.map((item, index) => {
			return !index ? item.label : `${item.label.split('~')[1]}:00`;
		})
		.join(' ');

	const newEnd = end
		.map((item, index) => {
			return !index ? item.label : `${item.label.split('~')[1]}:00`;
		})
		.join(' ');

	if (moment(newEnd).valueOf() <= moment(newStart).valueOf()) {
		uni.showToast({
			mask: true,
			icon: 'none',
			title: '预计发刊时间需大于探店时间'
		});
		return true;
	}
};
